<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class News extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
		if($this->session->userdata('admin_logged_in') == FALSE){
			redirect(base_url()."admin/login");
		}
		
		$this->load->model('admin/news_model','news_model');
		$result['news_list'] = $this->news_model->list_news(1,50);
		
		$data['menu_1'] = '';
		$data['menu_2'] = 'current';
		$data['menu_3'] = '';
		$data['menu_4'] = '';
		$data['menu_5'] = '';
		$this->load->view('admin/b_header_view');
		$this->load->view('admin/b_menu_view',$data);
		$this->load->view('admin/b_news_view' , $result);
		$this->load->view('admin/b_footer_view');
	}
	
	public function lists($p=1)
	{
		if($this->session->userdata('admin_logged_in') == FALSE){
			redirect(base_url()."admin/login");
		}
		
		$this->load->model('admin/news_model','news_model');
		$result['news_list'] = $this->news_model->list_news($p,50);
		
		$data['menu_1'] = '';
		$data['menu_2'] = 'current';
		$data['menu_3'] = '';
		$data['menu_4'] = '';
		$data['menu_5'] = '';
		$this->load->view('admin/b_header_view');
		$this->load->view('admin/b_menu_view',$data);
		$this->load->view('admin/b_news_view' , $result);
		$this->load->view('admin/b_footer_view');
	}
	
	public function add()
	{
		if($this->session->userdata('admin_logged_in') == FALSE){
			redirect(base_url()."admin/login");
		}
		
		$data['menu_1'] = '';
		$data['menu_2'] = 'current';
		$data['menu_3'] = '';
		$data['menu_4'] = '';
		$data['menu_5'] = '';
		$this->load->view('admin/b_header_view');
		$this->load->view('admin/b_menu_view',$data);
		$this->load->view('admin/b_news_add_view');
		$this->load->view('admin/b_footer_view');
	}
	
	public function add_process()
	{
		if($this->session->userdata('admin_logged_in') == FALSE){
			redirect(base_url()."admin/login");
		}
		
		if(isset($_POST['title']) AND $_POST['title']!='' AND isset($_POST['s_date']) AND $_POST['s_date']!='' AND isset($_POST['f_date']) AND $_POST['f_date']!=''){
			$input['title'] = $_POST['title'];
			$input['s_date'] = $_POST['s_date'];
			$input['f_date'] = $_POST['f_date'];
			$this->load->model('admin/news_model','news_model');
			$this->news_model->add_news($input);
			redirect(base_url()."admin/news");
		}else{
			
			redirect(base_url()."admin/news/add");
		}
	}
	
	public function edit($n_id)
	{
		if($this->session->userdata('admin_logged_in') == FALSE){
			redirect(base_url()."admin/login");
		}
		
		$this->load->model('admin/news_model','news_model');
		$result['news_info'] = $this->news_model->get_news_info($n_id);
		
		$data['menu_1'] = '';
		$data['menu_2'] = 'current';
		$data['menu_3'] = '';
		$data['menu_4'] = '';
		$data['menu_5'] = '';
		$this->load->view('admin/b_header_view');
		$this->load->view('admin/b_menu_view',$data);
		$this->load->view('admin/b_news_edit_view',$result);
		$this->load->view('admin/b_footer_view');
	}
	
	public function edit_process()
	{
		if($this->session->userdata('admin_logged_in') == FALSE){
			redirect(base_url()."admin/login");
		}
		
		if(isset($_POST['n_id']) AND isset($_POST['title']) AND $_POST['title']!='' AND isset($_POST['s_date']) AND $_POST['s_date']!='' AND isset($_POST['f_date']) AND $_POST['f_date']!=''){
			$input['n_id'] = $_POST['n_id'];
			$input['title'] = $_POST['title'];
			$input['s_date'] = $_POST['s_date'];
			$input['f_date'] = $_POST['f_date'];
			$this->load->model('admin/news_model','news_model');
			$this->news_model->update_news($input);
			redirect(base_url()."admin/news");
		}else{
			redirect(base_url()."admin/news");
		}
	}
	
	public function del($n_id)
	{
		if($this->session->userdata('admin_logged_in') == FALSE){
			redirect(base_url()."admin/login");
		}
		
		if(($n_id+0)>0){
			$this->load->model('admin/news_model','news_model');
			$this->news_model->delete_news($n_id+0);
			redirect(base_url()."admin/news");
		}else{
			redirect(base_url()."admin/news");
		}
	}
}

/* End of file news.php */
/* Location: ./application/controllers/news.php */